from django.shortcuts import render,redirect,get_object_or_404

from django.views import generic 
from django.utils import timezone
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from django.views.generic import UpdateView,CreateView
from django.urls import reverse_lazy
from django.utils.decorators import method_decorator


from .models import Task,TaskDealer

from .forms import TaskForm

# Create your views here.


class TaskIndexView(generic.ListView):
    template_name = 'Tasks/task_index.html'
    context_object_name = 'latest_task_list'

    def get_queryset(self):
        return Task.objects.filter(
            status='0',
        ).exclude(
            created_by = self.request.user,
        ).exclude(
            task_typ = '1',
        ).order_by('-created_at')

class MyTaskIndexView(generic.ListView):
    template_name = 'Tasks/my_task_index.html'
    context_object_name = 'latest_task_list'

    def get_queryset(self):
        return Task.objects.filter(
            dealer__dealer = self.request.user,
        ).exclude(
            task_typ = '1',
        ).order_by('-created_at')

def MyTaskIndexViewF(request):
    latest_task_list=Task.objects.filter(
            status='1',
            dealer__dealer = request.user,
        ).exclude(
            task_typ = '1',
        ).order_by('-created_at')
    return render(request,'Tasks/my_task_index.html',{'latest_task_list':latest_task_list})


class GiftIndexView(generic.ListView):
    template_name = 'Tasks/gift_index.html'
    context_object_name = 'latest_gift_list'

    def get_queryset(self):
        return Task.objects.filter(
            status='0',
            task_typ ='1',
        ).order_by('-created_at')


class DetailView(generic.DetailView):
    model = Task
    template_name = 'Tasks/task_detail.html'
    def get_context_data(self,**kwargs):
        context = super().get_context_data(**kwargs)
        context['now'] = timezone.now()
        return context

#@method_decorator(login_required, name='dispatch')
@login_required
def TaskDetailView(request,id):
    task = get_object_or_404(Task, pk=id)
    if request.method == 'POST':
        #task = get_object_or_404(Task,pk=id)
        if TaskDealer.objects.filter(task=task,dealer=request.user).count() == 0 :
            taskdealer=TaskDealer()
            taskdealer.task=task
            taskdealer.dealer = request.user
            taskdealer.point = task.point
            taskdealer.status='0'
            taskdealer.task.status='1'
            taskdealer.task.save()
            taskdealer.save()
            return redirect('Tasks:my_taskf') 
        else:
            return HttpResponse(status=403) 
    return render(request, 'Tasks/task_detail.html', {"task": task})

@login_required
def MyTaskDetailView(request,id):
    task = get_object_or_404(Task, pk=id)
    if request.method == 'POST':
        taskdealer = get_object_or_404(TaskDealer,task=task,dealer=request.user,status='0')
        committype=request.POST.get("optionsRadios",None)
        if committype == "giveup":
            taskdealer.status='3'
        if committype == "complate":
            taskdealer.status='2'
        taskdealer.save()
        return redirect('Tasks:my_taskf') 
    return render(request, 'Tasks/my_task_detail.html', {"task": task})


#@login_required
#class NewTaskView(request):
@method_decorator(login_required, name='dispatch')
class NewTaskView(CreateView):
    model = Task
    form_class = TaskForm
    success_url = reverse_lazy('task_list')
    template_name = 'Tasks/new_task.html'
    def form_valid(self,form):
        task = form.save(commit=False)
        task.created_by = self.request.user
        task.save()
        return redirect('Tasks:task_index')
        #return redirect('TaskIndexViewF')
#class NewTaskView(CreateView):
#    model = Task
#    form_class = TaskForm
#    success_url = reverse_lazy('task_list')
#    template_name  = 'new_task.html'
#     
#    def form_valid(self,form):
#        task = form.save(commit=False)
#        task.create_by = self.request.user
#        task.create_at = timezone.now()
#        task.save()
#    user =  request.user
#    if request.method == 'POST':
#        form = TaskForm(request.POST)
#        if form.is_valid():
#            task = form.save(commit=False)
#            task.created_by = user
#            task.save()
#            return redirect('task_index')
#    else:
#        form = TaskForm()
#    return render(request, 'Tasks/new_task.html', {'form': form})
